package com.xyy.saas.payment.service.report.impl;

import com.xyy.saas.payment.adapter.channel.fm.FuminAdapter;
import com.xyy.saas.payment.common.constants.ChannelProductConstants;
import com.xyy.saas.payment.dao.model.PayAccount;
import com.xyy.saas.payment.dao.model.ThirdUserInfo;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.manage.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.pojo.bo.QueryDayBalanceRecordBo;
import com.xyy.saas.payment.pojo.bo.QueryFuminRecordBo;
import com.xyy.saas.payment.pojo.domain.QueryAccountBalanceDO;
import com.xyy.saas.payment.pojo.domain.QueryMerchantDayBalanceDO;
import com.xyy.saas.payment.pojo.vo.QueryFuminRecordVo;
import com.xyy.saas.payment.service.PayAccountService;
import com.xyy.saas.payment.service.ThirdUserInfoService;
import com.xyy.saas.payment.service.report.QueryFuminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QueryFuminServiceImpl implements QueryFuminService {

    @Autowired
    private FuminAdapter fuminAdapter;

    @Autowired
    private PayAccountService payAccountService;

    @Autowired
    private ThirdUserInfoService thirdUserInfoService;

    @Override
    public QueryFuminRecordBo queryFuminRecord(QueryFuminRecordVo queryFuminRecordVo) throws PaymentException {
        QueryAccountBalanceDO queryAccountBalanceDO = new QueryAccountBalanceDO();
        queryAccountBalanceDO.setBeginTime(queryFuminRecordVo.getBeginTime());
        queryAccountBalanceDO.setEndTime(queryFuminRecordVo.getEndTime());
        PayAccount payAccount = payAccountService.getPayAccountByAccountId(queryFuminRecordVo.getAccountId());
        if(null == payAccount) {
            throw new PaymentException(ResultCodeEnum.THIRD_USER_ID_NOT_EXIST);
        }
        queryAccountBalanceDO.setBusinessIdType(payAccount.getBusinessIdType());
        queryAccountBalanceDO.setBusinessUserId(payAccount.getBusinessUserId());
        queryAccountBalanceDO.setBusinessOrderType(payAccount.getBusinessIdType());
        queryAccountBalanceDO.setPageNo(queryFuminRecordVo.getPageNo());
        queryAccountBalanceDO.setUserId(queryFuminRecordVo.getAccountId());
        //获取第三方userId
        ThirdUserInfo thirdUserInfo = thirdUserInfoService.getThirdUserInfoByUserId(payAccount.getAccountId());
        if (thirdUserInfo==null||thirdUserInfo.getThirdUserId()==null){
            throw new PaymentException(ResultCodeEnum.THIRD_USER_ID_NOT_EXIST);
        }
        queryAccountBalanceDO.setThirdUserId(thirdUserInfo.getThirdUserId());
        return fuminAdapter.queryBalanceRecord(queryAccountBalanceDO);
    }

    @Override
    public QueryDayBalanceRecordBo queryBalanceRecord(QueryMerchantDayBalanceDO queryMerchantDayBalanceDO) throws PaymentException {
        return fuminAdapter.queryDayBalanceRecord(queryMerchantDayBalanceDO);
    }

    @Override
    public QueryDayBalanceRecordBo queryMerchantRecord(QueryMerchantDayBalanceDO queryMerchantDayBalanceDO) throws PaymentException {
        return fuminAdapter.queryMerchantRecord(queryMerchantDayBalanceDO);
    }
}
